################################
#	John Henderson
#	Gerrymandering Incumbency 
#		(with Brian Hamel and Aaron Goldzimer)
#		January 1, 2018
#
################################
# code edits that produced by Chen & Cottrell (2016), to replicate their 2010 analysis 
#  for competitiveness outcomes                                                 
################################    
#  :: produces moments for simulated plans in 2010   
# function called by respective plotting functions, 
#  cd_2000-vert, ad_2000-vert, sd_2000-vert                              
#  cd_2000-flip, ad_2000-flip, sd_2000-flip

##################################################################################
######## SECTION 1: THIS SECTION DOWNLOADS ALL REPLICATION DATA ONTO A LOCAL FOLDER
##################################################################################
#rm(list=ls())

#library(stringr)  
#library(foreign)

setwd('~/Dropbox/StateRedistricting/replication/short/')
dir <- paste(getwd(), "/sims2010",sep="")

#####################################################################
######## SECTION 2: CREATE THE ELECTORAL BIAS FILE
#####################################################################

CrossStateFolder <- paste(dir, "",sep="")
    
##############################################################################
########## Code to Produce Data for Later Plotting 
##############################################################################
                  
plans=read.csv(paste(getwd(),'/redist_authority.csv',sep=''),stringsAsFactors=F)   

do.mean=T
if(do.mean==T){
	do.tau=F
} else{
	do.tau=T	
}
 
# produce a quasi-bayesian measure of flip probability :: prior(simulated vote margin) x data(counterfactual flip ratio) 
#load state swing data
load(paste(getwd(),'/stateSwings.Rdata',sep=''))  

flipPr=function(temp,state,year_mins,year_maxs,seats=T){
 	# assumes that temp is built from rshare-(1-rshare)
	rshare=(temp+1)/2    
	dshare=1-(temp+1)/2  
	           
	#imagine some feasible counterfactual
	i.state=which(rownames(year_mins)==state)    
	minR=year_mins[i.state,1]/100
	maxR=year_maxs[i.state,1]/100
	minD=year_mins[i.state,2]/100
	maxD=year_maxs[i.state,2]/100  
	
	updat=as.numeric((as.numeric(sign((dshare+abs(maxD-mean(dshare)))-(rshare-abs(minR-mean(rshare))))!=sign(dshare-rshare))+
	as.numeric(sign((rshare+abs(maxR-mean(rshare)))-(dshare-abs(minD-mean(dshare))))!=sign(rshare-dshare)))>0)
	
	if(seats==T){
		return(mean(updat))
	} else{
		dshare+abs(maxD-mean(dshare))
		rshare-abs(minR-mean(rshare))		
   
		rshare+abs(maxR-mean(rshare)) 	
		dshare-abs(minD-mean(dshare))
	}
	 
}     
  
states=c(
  "AL","AK","AZ","AR","CA",  "CO","CT","DE","FL",  "GA","HI","ID","IL","IN",  "IA","KS","KY","LA","ME",  "MT","NE","NV","NH","NJ", "OR",  
  "NM","NY","NC","ND","OH",  "OK","MD","MA","MI",  "MN","MS","MO","PA","RI",  "SC","SD","TN","TX","UT",  "VT","VA","WA","WV","WI", "WY")

states=sort(states)

plans10=plans[which(plans[,1]==2010),]
included=array(FALSE,nrow(plans10))
for(i in 1:length(states)){
	included[which(states[i]==plans10[,2])]=TRUE
}

plans10=plans10[included,]
plans10=plans10[order(plans10[,2]),]

plansCD10 = array(NA,length(states))
plansCD10[which(plans10$congress_court==1)]='C'
plansCD10[which(plans10$congress_independent==1)]='I'
plansCD10[which(plans10$congress_politician==1)]='B'
plansCD10[which(is.na(plansCD10) & plans10$congress_legislatue_partycontrol=='nonpartisan')]='B' # NE
plansCD10[which(is.na(plansCD10) & plans10$congress_legislatue_partycontrol=='split')]='B'
plansCD10[which(is.na(plansCD10) & plans10$congress_legislatue_partycontrol=='D')]='D'
plansCD10[which(is.na(plansCD10) & plans10$congress_legislatue_partycontrol=='R')]='R'
plansCD10[which(is.na(plansCD10))]='N'


states_ix=states[c(which(plansCD10=='D'),which(plansCD10=='R'),which(plansCD10=='B'),which(plansCD10=='C'),which(plansCD10=='I'))]  
                                
e08=read.table(paste(getwd(),'/sims2010/cd.txt',sep=''))
nms=e08[1,]
e08=as.data.frame(e08[-c(1),])
names(e08)=t(t(nms))

e08$mcnshare=as.numeric(as.character(e08[,10]))/(as.numeric(as.character(e08[,9]))+as.numeric(as.character(e08[,10])))                     
e08$dif=abs((1-e08[,11])-e08[,11])      

election08=tapply(e08$dif,e08[,1],mean)
election08=election08[-c(which(names(election08)=='st'))]
tt.cds=matrix(NA,1,201)
sims=matrix(NA,length(states),211)   

for(j in 1:length(states)){     

	fls=paste(getwd(),'/sims2010/',states[j],'/plans/',length(which(e08[,1]==states[j])),'.txt',sep='')   
	if(file.exists(fls)){
		cds=read.table(fls)[,-c(1)]
		if(length(dim(cds)[1])>0){
			 
			t.cds=cbind(states[j],t(abs((1-cds[1:200,])-cds[1:200,])))
		} else{
			t.cds=cbind(states[j],t(abs((1-cds[1:200])-cds[1:200]))) 			
		}
	} else if(!file.exists(fls)){
		fla=fls                        
		pp=0
		while(!file.exists(fla) & pp<60){
			pp=pp+1
			fla=paste(getwd(),'/sims2010/',states[j],'/plans/',pp,'.txt',sep='')   					
		}
		fls=fla 
		if(file.exists(fls)){ 
			cds=read.table(fls)[,-c(1)]           
			if(length(dim(cds)[1])>0){ 
				t.cds=cbind(states[j],t(abs((1-cds[1:200,])-cds[1:200,])))
			} else{
				t.cds=cbind(states[j],t(abs((1-cds[1:200])-cds[1:200]))) 			
			}  		     
		} else{  
			t.cds=t(c(states[j],rep(NA,200)))
			next(paste(states[j],'has no simulations',sep=' '))
		}
	}  
	tt.cds=rbind(tt.cds,t.cds)        

	sims[j,1]=states[j] 
	if(length(which(e08[,1]==states[j]))>1){
		sims[j,-c(1)][1:dim(cds)[1]]=rowMeans(abs((1-cds)-cds))
	} else{
		sims[j,-c(1)][1:length(cds)]=(abs((1-cds)-cds))
	}
}
    
tt.e08=cbind(as.character(e08$st),e08$dif)

sims=as.data.frame(sims[,1:201])
for(j in 2:ncol(sims)){
	sims[,j]=as.numeric(as.character(sims[,j]))
}


xx=table(e08[,1])
for(i in 1:length(xx)){
	if(xx[i]==1){
		sims[which(names(xx)[i]==sims[,1]),-c(1)]=0  
		election08[which(names(xx)[i]==names(election08))]=0
	}
}


is.even <- function(x) x %% 2 == 0 
          
# called by plotting function : next  
# throughput to plotting =>